package com.wdl.sm.webui.security;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.wdl.sm.webui.security.SUser;
import com.wdl.sm.webui.security.SUserRepository;

@RestController
public class RegisterController {
    @Autowired
    private SUserRepository repository;

    @RequestMapping(value = "/user/{userName:.+}", method = RequestMethod.GET)
    @ResponseBody
    public SUser getUser(@PathVariable("userName") String userName) {
        if (userName == null || userName.isEmpty()) {
            return null;
        }

        return this.repository.findByName(userName);
    }

    @RequestMapping(value = "/user/{userName:.+}", method = RequestMethod.DELETE)
    @ResponseBody
    public Map<String, String> removeUser(@PathVariable("userName") String userName) {
        Map<String, String> result = new HashMap<String, String>();

        if (userName == null || userName.isEmpty() || "admin".equals(userName)) {
            result.put("error", "用户名非法");
            return result;
        }

        SUser user = this.repository.findByName(userName);
        if (user != null) {
            this.repository.delete(user);
        }

        result.put("name", userName);

        return result;
    }

    @RequestMapping(value = "/user", method = RequestMethod.PUT)
    @ResponseBody
    public Map<String, String> updateUser(@RequestBody Map<String, String> user) {
        Map<String, String> result = new HashMap<String, String>();

        String name = user.get("name");
        String desc = user.get("desc");
        String tel = user.get("tel");
        String email = user.get("email");
        String role = user.get("role");
        String password = user.get("password");

        String error = "";
        if (name == null || name.isEmpty()) {
            error = "用户名为空";
            result.put("error", error);
            return result;
        }

        if (password == null || password.isEmpty()) {
            error = "密码为空";
            result.put("error", error);
            return result;
        }

        if (role == null || role.isEmpty()) {
            role = SUser.ROLE_VIEW;
        }

        desc = desc == null ? "" : desc;
        tel = tel == null ? "" : tel;
        email = email == null ? "" : email;

        repository.delete(name);
        repository.save(new SUser(name, desc, tel, email, role, password));

        return user;
    }

    @RequestMapping(value = "/user", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> register(@RequestBody Map<String, String> user) {
        Map<String, String> result = new HashMap<String, String>();

        String name = user.get("name");
        String desc = user.get("desc");
        String tel = user.get("tel");
        String email = user.get("email");
        String role = user.get("role");
        String password = user.get("password");

        String error = "";
        if (name == null || name.isEmpty()) {
            error = "用户名为空";
            result.put("error", error);
            return result;
        }

        if (password == null || password.isEmpty()) {
            error = "密码为空";
            result.put("error", error);
            return result;
        }

        if (role == null || role.isEmpty()) {
            role = SUser.ROLE_VIEW;
        }

        desc = desc == null ? "" : desc;
        tel = tel == null ? "" : tel;
        email = email == null ? "" : email;

        repository.save(new SUser(name, desc, tel, email, role, password));

        return user;
    }

    @RequestMapping(value = "/users", method = RequestMethod.GET)
    public List<SUser> getAllUsers() {
        return this.repository.findAll();
    }
}
